package com.health.manage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.manage.mapper.PhysicalMapper;
import com.health.manage.mapper.UserMapper;
import com.health.manage.pojo.dto.PhysicalDto;
import com.health.manage.pojo.entity.Physical;
import com.health.manage.pojo.entity.User;
import com.health.manage.service.IPhysicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 *
 * @Description: 身体指标业务处理
 * @author: physical
 */
@Service
public class PhysicalServiceImpl extends ServiceImpl<PhysicalMapper, Physical> implements IPhysicalService {

	@Autowired(required = false)
	private PhysicalMapper physicalMapper;
	@Autowired(required = false)
	private UserMapper userMapper;

	@Override
	public Boolean addData(Physical physical) {
		int result = this.physicalMapper.insert(physical);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Boolean updateData(Physical physical) {
		int result = this.physicalMapper.updateById(physical);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public Boolean deleteData(Long id) {
		int result = this.physicalMapper.deleteById(id);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Page<Physical> getPageData(PhysicalDto physicalDto) {

		Page<Physical> page = new Page<>(physicalDto.getCurrentPage(), physicalDto.getPageSize());
		QueryWrapper<Physical> queryWrapper = new QueryWrapper<>();
		if(physicalDto.getUserId()!=null && !"".equals(physicalDto.getUserId())){
			queryWrapper.eq("user_id", physicalDto.getUserId());
		}
		if(physicalDto.getHeight()!=null && !"".equals(physicalDto.getHeight())){
			queryWrapper.like("height", physicalDto.getHeight());
		}
		if(physicalDto.getAccount()!=null && !"".equals(physicalDto.getAccount())){
			queryWrapper.apply("user_id in(select id from user where account like {0} )", "%" + physicalDto.getAccount() + "%");
		}
		Page<Physical>records = this.physicalMapper.selectPage(page, queryWrapper);
		for(Physical physical : records.getRecords()){
			User user = this.userMapper.selectById(physical.getUserId());
			if(user != null){
				physical.setUser(user);
			}
			User creator = this.userMapper.selectById(physical.getCreateId());
			if(creator != null){
				physical.setCreator(creator);
			}
		}
		return records;
	}


}
